/**
 * 
 */
package com.yonder.agenda.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

import com.yonder.agenda.entity.adapter.ContactAdapter;
import com.yonder.agenda.entity.generic.AbstractEntity;

/**
 * {@link Email} entity.
 * 
 * @author CosminS
 * @since Jan 31, 2014
 */
@Entity
@Table(name = "emails")
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Email extends AbstractEntity {

	/**
	 * Contact for which the telephones belongs to.
	 */
	@ManyToOne
	@JoinColumn(name = "contact_id", nullable = false)
	@XmlJavaTypeAdapter(value = ContactAdapter.class)
	private Contact contact;

	/**
	 * JSON string for recording multiple telephone numbers.
	 */
	@Column(name = "email", nullable = false)
	private String email;
	
	public Contact getContact() {
		return contact;
	}

	public void setContact(Contact contact) {
		this.contact = contact;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((contact == null) ? 0 : (int) (contact.getId() ^ (contact.getId() >>> 32)));
		result = prime * result + ((email == null) ? 0 : email.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof Email)) {
			return false;
		}
		Email other = (Email) obj;
		if (contact == null) {
			if (other.contact != null) {
				return false;
			}
		} else if (!contact.equals(other.contact)) {
			return false;
		}
		if (email == null) {
			if (other.email != null) {
				return false;
			}
		} else if (!email.equals(other.email)) {
			return false;
		}
		return true;
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("Email [contact=");
		builder.append(contact == null ? 0 : contact.getId());
		builder.append(", email=");
		builder.append(email);
		builder.append("]");
		return builder.toString();
	}
}